package com.aaa.book_mall.controller;

import com.aaa.book_mall.dao.TypeDao;
import com.aaa.book_mall.dao.impl.TypeDaoImpl;
import com.aaa.book_mall.entity.Type;
import com.aaa.book_mall.util.LayTab;
import com.aaa.book_mall.util.Result;
import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * fileName:TypeServlet
 * description:
 * author:付亚星
 * createTime:2021/1/18 15:00
 * version:1.0.0
 */
@RestController
@RequestMapping("/type")
public class TypeController{
    private TypeDao typeDao = new TypeDaoImpl();

    /**
     * 查询所有的类型信息
     * @param resp
     * @throws IOException
     */
    @RequestMapping("/getAllType")
    public void getAllType( HttpServletResponse resp) throws IOException {
        List<Map<String, Object>> types = typeDao.getAllType();
        String s = JSON.toJSONString(types);
        resp.getWriter().write(s);
    }

    /**
     * 查询每种类型的本数
     * @param req
     * @return
     */
    @RequestMapping("/getNumberType")
    public LayTab getNumberType(HttpServletRequest req){
        //获取所有请求的参数 （包含表单中所有的参数）
        Map<String, String[]> map = req.getParameterMap();
        Type type = new Type();
        try {
            BeanUtils.populate(type,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        List<Map<String, Object>> numberType = typeDao.getNumberType(type);
        int count = typeDao.getTypeCount(type);
        LayTab layTab = new LayTab(count,numberType);
        return layTab;

    }

    /**
     * 新增
     * @param req
     * @return
     */
    @RequestMapping("/insertType")
    public Map insertType(HttpServletRequest req){
        Map<String, String[]> maps = req.getParameterMap();
        System.out.println(maps);
        Type type = new Type();
        try {
            BeanUtils.populate(type,maps);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println(type);
        int i = typeDao.insertType(type);

        HashMap<Object, Object> map = new HashMap<>();
        if(i>0){
            map.put("code","1");
            map.put("msg","修改成功");
        }else{
            map.put("code","0");
            map.put("msg","修改失败");
        }
      /*  Result result;

        if(i>0){
            result = new Result("1", "修改成功");
        }else{
            result = new Result("0", "修改失败");
        }*/
        return map;
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping("/deleteType")
    public Result deleteType(@RequestBody Type type){

        int i = typeDao.deleteType(type.getTypeid());
        Result result;
        //判断删除是否成功
        if(i>0){
            result = new Result("1","删除成功");
        }else {
            result = new Result("0", "删除失败");
        }
      return result;
    }
}
